<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class SecurityCenter
 * Desc  安全中心控制器
 * Create on 2024/10/22 16:08
 * Create by wangyafang
 */

namespace app\api\controller\wddj\user;


use app\api\model\wddj\Config;
use app\api\model\wddj\User;
use app\api\model\wddj\UserSoundRecordingSet;
use app\common\controller\Api;

class SecurityCenter extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['center','user_set_sound_recording'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * Desc 安全中心
     * Create on 2024/10/22 15:27
     * Create by wangyafang
     */
    public function center()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);
        $where['wechat_id'] = array('eq',$wechat_id);
        $emergencyContactCount = (new \app\api\model\wddj\user\EmergencyContact())->where($where)->count();
        $userRealNameAuthenticationObj = (new \app\api\model\wddj\user\UserRealNameAuthentication())->where($where)->find();

        $data = array(
            'emergency_contact_set_status'=>$emergencyContactCount > 0 ? 1 : 0,
            'certification_status'=>$userRealNameAuthenticationObj ? 1 : 0,
        );
        $configObj = (new Config())->get(1);
        $data['sound_recording_status'] = $configObj['sound_recording_status'];
        if ($configObj['sound_recording_status'] == 2) {
            $data['user_sound_recording_status'] = 2;
        } else {
            $userSoundRecordingSetModel = new UserSoundRecordingSet();
            $userSoundRecordingSetObj = $userSoundRecordingSetModel
                ->where('wechat_id',$wechat_id)
                ->find();
            if ($userSoundRecordingSetObj) {
                $data['user_sound_recording_status'] = $userSoundRecordingSetObj['status'];
            } else {
                $data['user_sound_recording_status'] = 2;
            }
        }
        $this->success('请求成功',$data);
    }

    /**
     * Desc 用户设置行程录音
     * Create on 2025/5/9 10:28
     * Create by wangyafang
     */
    public function user_set_sound_recording()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $status = $this->request->post('status');
        if (empty($status)) {
            $this->error('参数错误');
        }
        $wechat_id = User::getWechatId($this->auth->id);
        $userSoundRecordingSetModel = new UserSoundRecordingSet();
        $userSoundRecordingSetObj = $userSoundRecordingSetModel
            ->where('wechat_id',$wechat_id)
            ->find();
        if ($userSoundRecordingSetObj) {
            $userSoundRecordingSetObj->status = $status;
            $userSoundRecordingSetObj->save();
        } else {
            $create_data = array(
                'wechat_id'=>$wechat_id,
                'status'=>$status,
            );
            $userSoundRecordingSetModel->data($create_data);
            $userSoundRecordingSetModel->allowField(true)->save();
        }
        $this->success('设置成功');
    }
}



 